package org.yxing.palpalgps.activity;

import org.yxing.palpalgps.AbstractTabActivity;
import org.yxing.palpalgps.ActionBar;
import org.yxing.palpalgps.PalPalGPSApplication;
import org.yxing.palpalgps.R;
import org.yxing.palpalgps.services.XMPPService;
import org.yxing.palpalgps.utils.App;
import org.yxing.palpalgps.utils.ConfigHelper;
import org.yxing.palpalgps.utils.LocationTools;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.Toast;

public class HomeActivity extends AbstractTabActivity {
    private TabHost mTabHost;
    
    private static final String TAG = "HomeActivity";
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(getString(R.string.app_name));
        
        mTabHost = getTabHost();
        
        //group
        mTabHost.addTab(mTabHost.newTabSpec("group")
                .setIndicator(getString(R.string.group), getResources().getDrawable(R.drawable.ic_tab_group))
                .setContent(new Intent(this, GroupsActivity.class)));
        
        final TabSpec firend = mTabHost.newTabSpec("firend")
                .setIndicator(getString(R.string.friend), getResources().getDrawable(R.drawable.ic_tab_pal))
                .setContent(new Intent(this, FirendActivity.class));
        mTabHost.addTab(firend);
        
        //add recent chats
        mTabHost.addTab(mTabHost.newTabSpec("chat")
                .setIndicator(getString(R.string.chat), getResources().getDrawable(R.drawable.ic_tab_social))
                .setContent(new Intent(this, RecentChatsActivity.class)));
        
        mTabHost.addTab(mTabHost.newTabSpec("profile")
                .setIndicator(getString(R.string.profile), getResources().getDrawable(R.drawable.ic_tab_profile))
                .setContent(new Intent(this, ProfileActivity.class)));
        
        
        mTabHost.setCurrentTab(0);
        addActionBarItem(ActionBar.Type.Add);
        
        mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
            
            public void onTabChanged(String tabId) {
                clearActionBarItem();
                if (tabId == "group") {
                    addActionBarItem(ActionBar.Type.Add);
                } else if (tabId == "firend") {
                    addActionBarItem(ActionBar.Type.Refresh);
                } else if (tabId == "chat") {
                    addActionBarItem(ActionBar.Type.Group);
                }
            }
        });
        
        ConfigHelper.app = App.getInstance(this);
        
        PalPalGPSApplication application = (PalPalGPSApplication) getApplication();
        
        //location service
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        application.setLocationTools(new LocationTools(locationManager));
        
        //setup XMPPService
        Log.v(TAG, "start service Xmppservice");
        startService(new Intent(this, XMPPService.class));
    }
    
    public boolean onKeyUp(int keyCode, KeyEvent event) {
    	if (keyCode == KeyEvent.KEYCODE_BACK) {
    	    exit();
    		return false;
    	}
    	return super.onKeyUp(keyCode, event);
    }
    
    /**
     * exit application
     */
    public void exit() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
        .setTitle(getResources().getString(R.string.exit_app_message))
        .setIcon(getResources().getDrawable(android.R.drawable.ic_menu_help))
//      .setMessage(getResources().getString(R.string.exit_app_message))
        .setPositiveButton(getResources().getString(R.string.ok), new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which) {
                setResult(RESULT_OK);
                finish();
            }
        })
        .setNegativeButton(getResources().getString(R.string.cancel), null);
        builder.show();
    }
    
    public void displayToast(String text) {
        Toast.makeText(HomeActivity.this, "you hav new message", Toast.LENGTH_SHORT).show();
    }
    
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, 0, 0, R.string.logout).setIcon(getResources().getDrawable(R.drawable.ic_menu_logout));
        menu.add(0, 1, 1, R.string.exit).setIcon(R.drawable.ic_menu_exit);
        return true;
    }
    
    public void logout() {
        Intent intent = new Intent();
        intent.putExtra("logout", true);
        setResult(RESULT_OK, intent);
        finish();
    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case 0:
            logout();
            break;
        case 1:
            exit();
            break;
        }
        return true;
    }
}
